﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Common.Network.Helpers;

namespace MinecraftSharpServer
{
    public partial class Form1 : Form
    {
        private TcpListener server;
        Random rnd = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread tr = new Thread(StartServer);
            tr.Start();
        }

        public void StartServer()
        {
            server = new TcpListener(10000);
            server.Start();
            while(true)
            {
                Socket sock = server.AcceptSocket();
                Thread tr = new Thread(ProcessClient);
                tr.Start(sock);
            }
        }
        public void ProcessClient(object socket)
        {
            Socket sock = (Socket) socket;
            while(true)
            {
                try
                {
                    byte[] data = new byte[8192];
                    byte[] bytes = new byte[0];
                    sock.Receive(data);
                    int offset = 0;
                    byte packetType = PacketParser.GetByte(ref data, ref offset);
                    switch(packetType)
                    {
                        case 0://keep alive packet
                            PacketParser.PutByte(ref bytes,0);
                            PacketParser.PutInt(ref bytes, rnd.Next());
                            break;
                        case 1://login packet
                            //PacketParser.PutByte(ref bytes, 1);
                            //PacketParser.PutInt(ref bytes, rnd.Next());
                            //PacketParser.PutString(ref bytes, "");
                            //PacketParser.PutLong(ref bytes,1231412);
                            //PacketParser.PutInt(ref bytes, 0);
                            //PacketParser.PutByte(ref bytes, 0);
                            //PacketParser.PutByte(ref bytes, 128);
                            //PacketParser.PutByte(ref bytes, 20);
                            break;
                        case 2://handshake
                            //read name
                            string name = PacketParser.GetString(ref data, ref offset);
                            PacketParser.PutByte(ref bytes, 2);
                            PacketParser.PutString(ref bytes, name);
                            sock.Send(bytes);
                            bytes = new byte[0];
                            //
                            PacketParser.PutByte(ref bytes, 1);
                            PacketParser.PutInt(ref bytes, 14);
                            PacketParser.PutString(ref bytes, name, Encoding.UTF8);
                            PacketParser.PutLong(ref bytes,0L);
                            PacketParser.PutByte(ref bytes, 128);
                            break;
                    }
                    if(bytes.Length > 0)
                        sock.Send(bytes);
                    //process...
                }
                catch (Exception)
                {
                    try
                    {
                        sock.Close();
                    }
                    catch (Exception)
                    {
                        break;
                    }
                }
            }
        }
    }
}
